<?php
namespace haibao\app\data;

class Promotion extends \haibao\app\data\BaseMysql{

	public function __construct(){
		parent::__construct('\haibao\app\model\data\Promotion');
	}
	
	/**
	 * 添加推广数据
	 */
	public function addPromotion($model){
		$this->add($model);
		$data = array(
			'Id' => $model->Id,
			'type' => $model->DownloadType,
			'proname' => $model->ProName
		);
		return $data;
	}
	
	/**
	 * 添加推广点击记录
	 */
	public function addPromotionRecord($proId, $ClickIP){
		$sql = 'insert into PromotionRecord(`ProId`,`AddTime`,`ClickIP`) values (%s, %s, %s)';
		$this->query($sql,array(
			$proId, date('Y-m-d H:i:s'), $ClickIP
		));
	}
	
	/**
	 * 根据平台名称获取一条数据
	 */
	public function getPromotion($name,$seletType){
		$proList = array();
		$result = $this->query("select * from `Promotion` where ProName = %s and Status = %s and DownloadType = %s",array($name,\haibao\app\model\data\Promotion::STATUS_ENABLE,$seletType));
		$list = array();
		while($result->fetch_assoc()){
			$list['Id'] = $result->getData('Id');
			$list['ProName'] = $result->getData('ProName');
			$proList[] = $list;
		}
		return $proList;
	}
	
	/**
	 * 删除推广平台数据
	 */
	public function updateProStatus($id,$status = \haibao\app\model\data\Promotion::STATUS_DISABLE){
		$this->updateById($id, array(\haibao\app\model\filter\Promotion::CONDITION_FIELD_NAME_STATUS=>$status));
	}
	
	/**
	 * 获取平台名称
	 */
	public function getPromotionByName($name){
		if(empty($name)){
			$sql = "select Id, ProName, DownloadType from `Promotion` where Status = 1 order by Id DESC limit 10";
		}else{
			$sql = "select Id, ProName, DownloadType from `Promotion` where Status = 1 and `ProName` like %s limit 10";
		}
		$result = $this->query($sql,array("%".$name."%"));
		$data = $tmp = array();
		$downloadType = '';
		while ($result->fetch_assoc()){
			$tmp['Id'] = $result->getData('Id');
			if($result->getData('DownloadType') == \haibao\app\model\data\Promotion::DOWNLOADTYPE_ANDROID){
				$downloadType = 'Android';
			}elseif($result->getData('DownloadType') == \haibao\app\model\data\Promotion::DOWNLOADTYPE_IOS){
				$downloadType = 'IOS';
			}else{
				$downloadType = '自动识别';
			}
			$tmp['ProName'] = $result->getData('ProName').'('.$downloadType.')';
			array_push($data, $tmp);
		}
		
		return $data;
	}
	
}
